<script src="__STATIC__/plugins/layer/layer.js"></script>
<div class="row">
    <div class="col-md-12">
        <div class="box">
            {include file="template/form_header" /}
            <form id="dataForm" class="dataForm form-horizontal" method="post" onsubmit="return upload_file()" enctype="multipart/form-data">
                <div class="box-body">
                    <div class="fields-group">
                        <div class="form-group">
                            <label for="file" class="col-sm-2 control-label">上传文件</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-file-archive-o"></i></span>
                                    <input  type="file" name="file" id="file" class="form-control" placeholder="请上传文件">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                {include file="template/form_footer" /}
            </form>
        </div>
        <div class="box">
            <div class="box-header">
                <h3 class="box-title">上传结果</h3>
            </div>
            <div class="box-body">
                <div style="text-align: center">
                    <img  src="" id="result">
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function upload_file() {
        var formData =new FormData();
        formData.append("file",$("#file")[0].files[0]);
        $.ajax({
            url: '',
            type: "post",
            data: formData,
            async:false,
            dataType: 'json',
            cache: false,
            processData : false,
            contentType : false,
            success: function (result) {
                console.log(result);
                if (result.code == 1) {
                    console.log(result.data);
                    $("#result").attr('src', result.data + "?" + img_name_random());
                } else {
                    toastr.error(result.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                toastr.error("系统异常，请稍后重试");
            }
        });
        return false;
    }

    //图片后缀随机数
    function img_name_random() {
        var rand_one = parseInt(100 * Math.random());
        var rand_two = parseInt(100 * Math.random());
        return String(rand_one) + String(rand_two);
    }
</script>